﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shape
{
    // 定义形状接口
    public interface IShape
    {
        double Area { get; }
        bool IsValid { get; }
    }

    // 抽象形状类
    public abstract class Shape : IShape
    {
        public abstract double Area { get; }
        public abstract bool IsValid { get; }
    }

    // 长方形类
    public class Rectangle : Shape
    {
        public double Width { get; set; }
        public double Height { get; set; }

        public Rectangle(double width, double height)
        {
            Width = width;
            Height = height;
        }

        public override double Area
        {
            get { return Width * Height; }
        }

        public override bool IsValid
        {
            get { return Width > 0 && Height > 0; }
        }
    }

    // 正方形类
    public class Square : Shape
    {
        public double Side { get; set; }

        public Square(double side)
        {
            Side = side;
        }

        public override double Area
        {
            get { return Side * Side; }
        }

        public override bool IsValid
        {
            get { return Side > 0; }
        }
    }

    // 三角形类
    public class Triangle : Shape
    {
        public double Side1 { get; set; }
        public double Side2 { get; set; }
        public double Side3 { get; set; }

        public Triangle(double side1, double side2, double side3)
        {
            Side1 = side1;
            Side2 = side2;
            Side3 = side3;
        }

        public override double Area
        {
            get
            {
                if (!IsValid) return 0;
                double s = (Side1 + Side2 + Side3) / 2;
                return Math.Sqrt(s * (s - Side1) * (s - Side2) * (s - Side3));
            }
        }

        public override bool IsValid
        {
            get
            {
                return Side1 > 0 && Side2 > 0 && Side3 > 0 &&
                       Side1 + Side2 > Side3 &&
                       Side1 + Side3 > Side2 &&
                       Side2 + Side3 > Side1;
            }
        }
    }

    // 简单工厂类
    public class ShapeFactory
    {
        private static Random random = new Random();

        public static Shape CreateRandomShape()
        {
            int shapeType = random.Next(3);
            switch (shapeType)
            {
                case 0:
                    double width = random.NextDouble() * 10 + 1;
                    double height = random.NextDouble() * 10 + 1;
                    return new Rectangle(width, height);
                case 1:
                    double side = random.NextDouble() * 10 + 1;
                    return new Square(side);
                case 2:
                    double side1 = random.NextDouble() * 10 + 1;
                    double side2 = random.NextDouble() * 10 + 1;
                    double side3 = random.NextDouble() * 10 + 1;
                    return new Triangle(side1, side2, side3);
                default:
                    return null;
            }
        }
    }

    class Program
    {
        static void Main()
        {
            double totalArea = 0;
            for (int i = 0; i < 10; i++)
            {
                Shape shape = ShapeFactory.CreateRandomShape();
                if (shape.IsValid)
                {
                    totalArea += shape.Area;
                }
            }
            Console.WriteLine($"10 个形状对象的有效面积之和为: {totalArea}");
        }
    }
}

